import 'package:flutter/material.dart';

extension $FocusNode on FocusNode {
  void doActionWithoutFocus(VoidCallback? action,
      {bool keepIfHasFocus = true}) {
    if (this.hasFocus && keepIfHasFocus != false) {
      action?.call();
      return;
    }

    // Unfocus all focus nodes
    this.unfocus();

    // Disable text field's focus node request
    this.canRequestFocus = false;

    // Do your stuff
    action?.call();

    //Enable the text field's focus node request after some delay
    Future.delayed(Duration(milliseconds: 100), () {
      this.canRequestFocus = true;
    });
  }

  void autoRequestFocusAfterInitState({int delay = 750}) {
    WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
      Future.delayed(Duration(milliseconds: delay), () {
        this.requestFocus();
      });
    });
  }
}
